package cn.chengpengper.unit19.client.console;

import cn.chengpengper.unit19.util.SessionUtil;
import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;

/**
 * @author huchengpeng
 */
public class ConsoleCommandManager implements ConsoleCommand {

    private final Map<String,ConsoleCommand> consoleCommandMap;

    public ConsoleCommandManager(){
        consoleCommandMap = new HashMap<>();
        consoleCommandMap.put("sendToUser",new SendToUserConsoleCommand());
        consoleCommandMap.put("createGroup", new CreateGroupConsoleCommand());
        consoleCommandMap.put("joinGroup", new JoinGroupConsoleCommand());
        consoleCommandMap.put("quitGroup", new QuitGroupConsoleCommand());
        consoleCommandMap.put("listGroupMembers", new ListGroupMembersConsoleCommand());
        consoleCommandMap.put("sendToGroup", new SendToGroupConsoleCommand());
    }
    @Override
    public void exec(Scanner scanner, Channel channel) {
        String command = scanner.next();
        if(!SessionUtil.hasLogin(channel)){
            return;
        }
        ConsoleCommand consoleCommand = consoleCommandMap.get(command);
        if(Objects.nonNull(consoleCommand)){
            consoleCommand.exec(scanner,channel);
        }else{
            System.out.println("无法识别[" + command + "]指令，请重新输入!");
        }
    }
}
